package com.huawei.lynnbit.tool.cocos.config.game;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public enum CocosVersion {
    COCOS2("2.x"), COCOS3("3.x");
    private static Map<String, CocosVersion> namesMap = new HashMap<>();

    static {
        namesMap.put(COCOS2.name, COCOS2);
        namesMap.put(COCOS3.name, COCOS3);
    }

    private String name;

    CocosVersion(String name) {
        this.name = name;
    }

    @JsonCreator
    public static CocosVersion forName(String name) {
        if (Objects.nonNull(namesMap.get(name.toLowerCase(Locale.ROOT)))) {
            return namesMap.get(name.toLowerCase(Locale.ROOT));
        }

        if (name.startsWith("2")) {
            return COCOS2;
        } else if (name.startsWith("3")) {
            return COCOS3;
        } else {
            return null;
        }
    }

    @JsonValue
    public String toName() {
        return name;
    }
}

